package com.mdp.menu.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdp.core.entity.Result;
import com.mdp.core.entity.Tips;
import com.mdp.core.query.QueryTools;
import com.mdp.menu.entity.MenuFavorite;
import com.mdp.menu.service.MenuFavoriteService;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * url编制采用rest风格,如对menu_favorite 菜单收藏夹的操作有增删改查,对应的url分别为:<br>
 *  新增: menu/menuFavorite/add <br>
 *  查询: menu/menuFavorite/list<br>
 *  模糊查询: menu/menuFavorite/listKey<br>
 *  修改: menu/menuFavorite/edit <br>
 *  删除: menu/menuFavorite/del<br>
 *  批量删除: menu/menuFavorite/batchDel<br>
 * 组织 com  顶级模块 mdp 大模块 menu 小模块 <br>
 * 实体 MenuFavorite 表 menu_favorite 当前主键(包括多主键): favoriteid; 
 ***/
@RestController("mdp.menu.menuFavoriteController")
@RequestMapping(value="/**/menu/menuFavorite")
@Api(tags={"菜单收藏夹操作接口"})
public class MenuFavoriteController {
	
	static Logger logger =LoggerFactory.getLogger(MenuFavoriteController.class);
	
	@Autowired
	private MenuFavoriteService menuFavoriteService;
	 
	@ApiOperation( value = "查询菜单收藏夹信息列表",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200,response=MenuFavorite.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listMenuFavorite( @ApiIgnore @RequestParam Map<String,Object>  menuFavorite){
		
		
		
		IPage page=QueryTools.initPage(menuFavorite);
		User user= LoginUtils.getCurrentUserInfo();
		QueryWrapper<MenuFavorite> qw= QueryTools.initQueryWrapper(MenuFavorite.class,menuFavorite);
		qw.eq("userid",user.getUserid());
		List<Map<String,Object>> menuFavoriteList = menuFavoriteService.selectListMapByWhere(page,qw,menuFavorite);
		
		return Result.ok().setData(menuFavoriteList).setTotal(page.getTotal());
	}


	/**
	 * 快捷入口的添加 保存用户设置的快捷入口
	 * @author 林钰坤
	 * @cdate 2019/1/24 20:10
	 * @params 用户需要保存的快捷入口的对象
	 */
	@RequestMapping(value = "/saveMenuFavorites", method = RequestMethod.POST)
	public Map<String, Object> saveMenuFavorites(@RequestBody Map<String, Object> params) {
		Map<String, Object> m = new HashMap<>();
		try {
			menuFavoriteService.saveMenuFavorites(params); // 列出MenuFavorite列表
			return Result.ok();
		} catch (Exception e) {// 吧错误的信息返回去
 			logger.error("执行异常",e);
			return Result.error(e);
		}
	}

	@RequestMapping(value = "/batchSaveMenuFavorites", method = RequestMethod.POST)
	public Map<String, Object> batchSaveMenuFavorites(@RequestBody Map<String, Object> params) {
		Map<String, Object> m = new HashMap<>();
 		try {
			List<Map<String,Object>> data = (List<Map<String, Object>>) params.get("data");
			User user= LoginUtils.getCurrentUserInfo();
			menuFavoriteService.batchSaveMenuFavorites(data, user.getUserid());	// 列出MenuFavorite列表
			return Result.ok();
		} catch (Exception e) {	// 吧错误的信息返回去
			logger.error("执行异常",e);
			return Result.error(e);
		}
	}





	/**
	 * 得到快捷入口所有的路径
	 * @author 林钰坤
	 * @cdate 2019/1/24 20:50
	 */
	@RequestMapping(value = "/getMenuFavorites", method = RequestMethod.POST)
	public Map<String, Object> getMenuFavorites(@RequestBody Map<String, Object> params) { 
		try {
			User user= LoginUtils.getCurrentUserInfo();
			List<MenuFavorite> data=menuFavoriteService.getMenuFavorites(user.getUserid()); // 列出MenuFavorite列表
			return Result.ok().setData( data);
		} catch (Exception e) {// 吧错误的信息返回去
 			logger.error("执行异常",e);
			return Result.error(e);
		} 
	}

	/**
	 * 新增快捷入口的图标
	 * @author 林钰坤
	 * @cdate 2019/1/25 14:02
	 */
	@RequestMapping(value = "/addMenuFavoriteIcon", method = RequestMethod.POST)
	public Map<String, Object> addMenuFavoriteIcon(@RequestBody Map<String, Object> params) {
		Map<String, Object> m = new HashMap<>();
		Tips tips = new Tips("新增图标成功");// 假设查询成功
		try {
			menuFavoriteService.addMenuFavoriteIcon(params); // 列出MenuFavorite列表
			return Result.ok();
		} catch (Exception e) {// 吧错误的信息返回去
 			logger.error("执行异常",e);
			return Result.error(e);
		}
	}

}
